package cn.iocoder.yudao.module.system.service.cylinderinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.cylinderinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.cylinderinfo.CylinderInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.cylinderinfo.CylinderInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 钢瓶信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CylinderInfoServiceImpl implements CylinderInfoService {

    @Resource
    private CylinderInfoMapper cylinderInfoMapper;

    @Override
    public Long createCylinderInfo(CylinderInfoSaveReqVO createReqVO) {
        // 插入
        CylinderInfoDO cylinderInfo = BeanUtils.toBean(createReqVO, CylinderInfoDO.class);
        cylinderInfoMapper.insert(cylinderInfo);
        // 返回
        return cylinderInfo.getId();
    }

    @Override
    public void updateCylinderInfo(CylinderInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateCylinderInfoExists(updateReqVO.getId());
        // 更新
        CylinderInfoDO updateObj = BeanUtils.toBean(updateReqVO, CylinderInfoDO.class);
        cylinderInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteCylinderInfo(Long id) {
        // 校验存在
        validateCylinderInfoExists(id);
        // 删除
        cylinderInfoMapper.deleteById(id);
    }

    private void validateCylinderInfoExists(Long id) {
        if (cylinderInfoMapper.selectById(id) == null) {
            throw exception(CYLINDER_INFO_NOT_EXISTS);
        }
    }

    @Override
    public CylinderInfoDO getCylinderInfo(Long id) {
        return cylinderInfoMapper.selectById(id);
    }

    @Override
    public PageResult<CylinderInfoDO> getCylinderInfoPage(CylinderInfoPageReqVO pageReqVO) {
        return cylinderInfoMapper.selectPage(pageReqVO);
    }

}